﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using DataAccess;

namespace InsuranceVehicle.AdminSite
{
    public partial class AddEmployee : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                addListItem();
            }

            EmployeeInfo emp = new EmployeeInfo();
            emp = (EmployeeInfo)Session["Employee"];
            if (emp != null)
            {
                if (emp.Department == 1)
                {
                    Menu1.ShowElement = "all";
                }
                else
                {
                    Menu1.ShowElement = "no";
                }
            }
            else
            {
                Response.Redirect("LoginAdmin.aspx");
            }            
        }

        private void addListItem()
        {
            for (int i = 1; i <= 12; i++)
            {
                cbbMonth.Items.Add(i.ToString());
            }

            for (int i = 1; i <= 31; i++)
            {
                cbbDay.Items.Add(i.ToString());
            }

            int curYear = DateTime.Now.Year;
            for (int i = curYear - 80; i <= curYear - 18; i++)
            {
                cbbYear.Items.Add(i.ToString());
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Page.Validate();
            EmployeeDataAccess db = new EmployeeDataAccess();
            EmployeeInfo emp = new EmployeeInfo();
            //lblError.Visible = false;
            lblErrAccount.Visible = false;
            lblErrEmail.Visible = false;
            if (IsValid)
            {
                emp.Firstname = txtFirstname.Text;
                emp.Lastname = txtLastname.Text;
                emp.Account = txtAccount.Text;
                emp.Password = txtPassword.Text;
                
                emp.Dob = cbbMonth.SelectedItem.ToString() + "/" +
                    cbbDay.SelectedItem.ToString() + "/" + cbbYear.SelectedItem.ToString();
                emp.Address = txtAddress.Text;
                emp.Phone = txtPhone.Text;
                emp.Email = txtEmail.Text;
                emp.Department = 0;
                int errCode = db.EmployeeRegister(emp);

                switch (errCode)
                {
                    case 1:
                        Response.Write("<script>alert('Employee register successful')</script>");
                        break;
                    case 2:
                        Response.Write("<script>alert('Connection error with database ')</script>");
                        break;
                    case 3:
                        lblErrAccount.Visible = true;
                        lblErrAccount.Text = "Account is existed";
                        txtAccount.Focus();
                        break;
                    case 4:
                        lblErrEmail.Visible = true;
                        lblErrEmail.Text = "Email is duplicated";
                        txtEmail.Focus();
                        break;
                    default:
                        lblErrAccount.Text = "";
                        lblErrEmail.Text = "";
                        break;
                }
                
            }
           
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtFirstname.Text = "";
            txtLastname.Text = "";
            txtAccount.Text = "";
            txtPassword.Text = "";
            txtConPass.Text = "";
            txtAddress.Text = "";
            txtPhone.Text = "";
            txtEmail.Text = "";
        }


    }
}